package org.raphaello.davinci;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsoluteLayout;
import android.widget.FrameLayout;

public class MovableLayout extends ViewGroup {

	public MovableLayout(Context context) {
		super(context);
	}
	
	public MovableLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int count = getChildCount();
		
		int width = 0;
		int height = 0;
		
		for(int i = 0; i < count; i++) {
			View c = getChildAt(i);
			
			width = c.getMeasuredWidth();
			height = c.getMeasuredHeight();
		}
		
		width = Math.max(width, getSuggestedMinimumWidth());
		height = Math.max(height, getSuggestedMinimumHeight());
		
		setMeasuredDimension(getDefaultSize(width, widthMeasureSpec), getDefaultSize(height, heightMeasureSpec));
	}
	
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		//super.onLayout(changed, l ,t , r, b);
		
		for(int i = 0; i < getChildCount(); i++) {
			View c = getChildAt(i);
			
			c.layout(0, 0, c.getMeasuredWidth(), c.getMeasuredHeight());
		}
	}
	
	@Override
	protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
		return new LayoutParams(0, 0);
	}
	
	@Override
	public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
		return new MovableLayout.LayoutParams(getContext(), attrs);
	}
	
	@Override
	protected boolean checkLayoutParams(ViewGroup.LayoutParams params) {
		return params instanceof MovableLayout.LayoutParams;
	}
	
	public static class LayoutParams extends ViewGroup.LayoutParams {

		public LayoutParams(int width, int height) {
			super(width, height);
		}
		
		public LayoutParams(Context context, AttributeSet attrs) {
			super(context, attrs);
		}
		
		public LayoutParams(ViewGroup.LayoutParams source) {
			super(source);
		}
	}
}
